<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : FilePlayer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">FilePlayer</p>
    	
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="fileplayer_method_channelcount.html" title="Returns the number of channels this UGen has been configured to generate.">channelCount ( )</a><br/>
    
    <a href="fileplayer_method_close.html" title="Calling close will close the AudioRecordingStream that this wraps, 
 which is proper cleanup for using the stream.">close ( )</a><br/>
    
    <a href="fileplayer_method_cue.html" title="Sets the position to <code>millis</code> milliseconds from
 the beginning.">cue ( )</a><br/>
    
    <a href="fileplayer_method_getlastvalues.html" title="Return the last values generated by this UGen. This will most often be
 used by sub-classes when pulling data from their inputs.">getLastValues ( )</a><br/>
    
    <a href="fileplayer_method_getmetadata.html" title="Returns the meta data for the recording being played by this player.">getMetaData ( )</a><br/>
    
    <a href="fileplayer_method_getstream.html" title="Returns the underlying AudioRecordingStream.">getStream ( )</a><br/>
    
    <a href="fileplayer_method_islooping.html" title="Returns true if this is currently playing and has more than one loop 
 left to play.">isLooping ( )</a><br/>
    
    <a href="fileplayer_method_isplaying.html" title="Returns true if this currently playing.">isPlaying ( )</a><br/>
    
    <a href="fileplayer_method_length.html" title="Returns the length of the sound in milliseconds.">length ( )</a><br/>
    
    <a href="fileplayer_method_loop.html" title="Start looping playback of the file.">loop ( )</a><br/>
    
    <a href="fileplayer_method_loopcount.html" title="Returns the number of loops left to do.">loopCount ( )</a><br/>
    
    <a href="fileplayer_method_patch.html" title="Send the output of this UGen to another UGen, UGenInput, or AudioOutput.">patch ( )</a><br/>
    
    <a href="fileplayer_method_pause.html" title="Pauses playback.">pause ( )</a><br/>
    
    <a href="fileplayer_method_play.html" title="Starts playback from the current position. 
 If this was previously set to loop, looping will be disabled.">play ( )</a><br/>
    
    <a href="fileplayer_method_position.html" title="Returns the current position of the "playhead" (ie how much of
 the sound has already been played)">position ( )</a><br/>
    
    <a href="fileplayer_method_printinputs.html" title="Prints all inputs connected to this UGen (for debugging)">printInputs ( )</a><br/>
    
    <a href="fileplayer_method_rewind.html" title="Rewinds to the beginning. This <i>does not</i> stop playback.">rewind ( )</a><br/>
    
    <a href="fileplayer_method_samplerate.html" title="Returns the sample rate of this UGen.">sampleRate ( )</a><br/>
    
    <a href="fileplayer_method_setchannelcount.html" title="Let this UGen know how many channels of audio you will be asking it for.">setChannelCount ( )</a><br/>
    
    <a href="fileplayer_method_setlooppoints.html" title="Sets the loop points used when looping.">setLoopPoints ( )</a><br/>
    
    <a href="fileplayer_method_setsamplerate.html" title="Set the sample rate for this UGen.">setSampleRate ( )</a><br/>
    
    <a href="fileplayer_method_skip.html" title="Skips <code>millis</code> from the current position.">skip ( )</a><br/>
    
    <a href="fileplayer_method_tick.html" title="Generates one sample frame for this UGen.">tick ( )</a><br/>
    
    <a href="fileplayer_method_unpatch.html" title="Unpatch this UGen from an AudioOutput or other UGen.">unpatch ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	The FilePlayer UGen provides a way for you to play audio files in the same 
 way that AudioPlayer does, allowing you to patch them into a UGen graph any way you choose.
 The constructor for FilePlayer takes an AudioRecordingStream,
 which you can get from a Minim object by calling the loadFileStream method.
    	<p class="memberSectionHeader">Constructors</p>
    	<pre><em>Construct a FilePlayer that will read from iFileStream.</em>
FilePlayer(AudioRecordingStream iFileStream)
</pre>
    	
   <p class="memberSectionHeader">Parameters</p>
   
        <span class="parameterName">iFileStream</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">AudioRecordingStream: the stream this should read from</span><br/>
    
   
    	<p class="memberSectionHeader">Related</p>
    	<A href="minim_class_minim.html">Minim</A><BR>
<A href="audioplayer_class_audioplayer.html">AudioPlayer</A><BR>
<A href="ugen_class_ugen.html">UGen</A><BR>

    	<p class="memberSectionHeader">Example</p>
    	<pre>/**
  This is an example of how to use a FilePlayer UGen to play an audio file. It support all of the same formats that 
  AudioPlayer does, but allows you to insert the audio from the file into a UGen chain. FilePlayer provides all the 
  same methods that AudioPlayer does for controlling the file playback: play(), loop(), cue(int position), etc.
  &lt;p>
  Press any key to pause and unpause playback!
  &lt;p>
  For more information about Minim and additional features, visit http://code.compartmental.net/minim/
  &lt;p>  
  author: Damien Di Fede
*/

import ddf.minim.*;
import ddf.minim.spi.*; // for AudioRecordingStream
import ddf.minim.ugens.*;

// declare everything we need to play our file
Minim minim;
FilePlayer filePlayer;
AudioOutput out;

// you can use your own file by putting it in the data directory of this sketch
// and changing the value assigned to fileName here.
String fileName = "groove.mp3";

void setup()
{
  // setup the size of the app
  size(640, 240);
  
  // create our Minim object for loading audio
  minim = new Minim(this);
                                                  
  // a FilePlayer reads from an AudioRecordingStream, which we 
  // can easily get from Minim using loadFileStream
  filePlayer = new FilePlayer( minim.loadFileStream(fileName) );
  // and then we'll tell the file player to loop indefinitely
  filePlayer.loop();
  
  // get a line out from Minim. It's important that the file is the same audio format 
  // as our output (i.e. same sample rate, number of channels, etc).
  out = minim.getLineOut();
  
  // patch the file player to the output
  filePlayer.patch(out);
                        
}

// keyPressed is called whenever a key on the keyboard is pressed
void keyPressed()
{
  // you can query whether the file is playing or not
  // playing simply means that it is generating sound
  // this will be true if you tell it to play() or loop()
  if ( filePlayer.isPlaying() )
  {
    // pauses playback of the file
    filePlayer.pause();
  }
  else
  {
    // starts the file looping again, picking up where we left off
    filePlayer.loop();
  }
}

void mousePressed()
{
  float pos = map(mouseX, 0, width, 0, filePlayer.length());
  filePlayer.cue((int)pos);
}


// draw is run many times
void draw()
{
  // erase the window to black
  background( 0 );
  // draw using a white stroke
  stroke( 255 );
  // draw the waveforms
  for( int i = 0; i &lt; out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }  

  float songPos = map( filePlayer.position(), 0, filePlayer.length(), 0, width );

  stroke( 255, 0, 0 );
  line( songPos, 0, songPos, height );
  
  text( "loopCount: " + filePlayer.loopCount(), 15, 15 );
}</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>